package com.sfh.cpc.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.entity.ECity;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.req.ReqUserUpdate;
import com.sfh.cpc.com.ui.SelectTakeImageActivity;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.user.model.UserModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;

import java.util.ArrayList;
import java.util.List;
import com.sfh.cpc.R;
public class UserInfoActivity extends SelectTakeImageActivity<UserModel> {

    private WidgetBaseToolbar toolbar;
    private ImageView ivHeader;
    private EditText etName;
    private TextView tvCity;
    private RadioGroup sexGroup ;
    private ReqUserUpdate reqUserUpdate;

    public static void startUI(Context context) {

        Intent intent = new Intent(context, UserInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_info_activity);
        initView();
    }

    private void initView() {
        toolbar =  findViewById(R.id.toolbar);
        ivHeader =  findViewById(R.id.iv_header);
        etName =  findViewById(R.id.et_name);
        tvCity =  findViewById(R.id.tv_city);
        sexGroup = findViewById(R.id.sex);

        toolbar.setTitle("个人信息");
        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getViewModel().queryCiry();
            }
        });
        ivHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              hasPermissions();
            }
        });
        findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name = etName.getText().toString();
                if (TextUtils.isEmpty(name)){
                    showToast("请输入昵称");
                    return;
                }
                String sex = sexGroup.getCheckedRadioButtonId() ==  R.id.sex_1?"1":"0";
                String address = tvCity.getText().toString();

                reqUserUpdate.setName(name);
                reqUserUpdate.setSex(sex);
                reqUserUpdate.setAddress(address);
                getViewModel().updateUserInfo(reqUserUpdate);
            }
        });
        reqUserUpdate = new ReqUserUpdate();
        this.getViewModel().getUser();
    }
    @Override
    public void onSelectImageOK(List<String> select) {

        if (select.isEmpty()){
            return;
        }
        this.getViewModel().upHeadFile(select.get(0));
    }

    @LiveDataMatch
    public void onUser(EUser user) {

        reqUserUpdate.setHeaderURL(user.getUserUrl());
        reqUserUpdate.setAddress(user.getUserAddress());
        reqUserUpdate.setSex(user.getUserSex());
        reqUserUpdate.setName(user.getUserName());

        sexGroup.check(TextUtils.equals("0", user.getUserSex()) ? R.id.sex_0 : R.id.sex_1);
        tvCity.setText(user.getUserAddress());
        etName.setText(user.getUserName());
        new ImageLoaderEngine(ivHeader,  BaseAppContent.getImageUrl(user.getUserUrl())).roundTransformCenterCrop(7).into(ivHeader);

    }
    @LiveDataMatch
    public void onHeaderSuccess(String file){
        ImageView iv = findViewById(R.id.iv_result);
        iv.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(file)){
            iv.setImageResource(R.drawable.meet_iocn_remind);
            return;
        }
        iv.setImageResource(R.drawable.ic_coupon_check_top_right);
        reqUserUpdate.setHeaderURL(file);
        new ImageLoaderEngine(ivHeader, BaseAppContent.getImageUrl(file)).roundTransformCenterCrop(7).into(ivHeader);
    }
    @LiveDataMatch
    public void  onUpdateSuccess(){
        showToast("更新成功");
        finish();
    }

    @LiveDataMatch
    public void selectCity(final List<ECity> data) {

        final List<List<ECity>> options2Items = new ArrayList<>();

        for (ECity city : data) {
            // 市
            options2Items.add(city.getChildList());
        }


        //条件选择器
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = options2Items.get(options1).get(option2).getAreaNm();
                if (TextUtils.equals("市辖区",tx)){
                    tx = data.get(options1).getAreaNm();
                }
                tvCity.setText(tx);
            }
        }).build();
        pvOptions.setPicker(data, options2Items);
        pvOptions.show();
    }

}
